package DMSXL._2023Before.HashTable;

/*public class fourSum_ {
    public static void main(String[] args) {
        int [] nums = {-2,-1,-1,1,1,2,2};
        Solution solution = new Solution();

        List<List<Integer>> matrix = new ArrayList<>();
        matrix = solution.fourSum(nums,0);
        for (List<Integer> row : matrix) {
            for (int num : row) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
}
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> ans = new ArrayList();
        int len = nums.length;
        if(nums == null || len < 4)
            return ans;

        HashMap<Integer, Integer> hashMap = new HashMap<>();
        Arrays.sort(nums);

        for (int i = 0; i < len; i++) {
            if(hashMap.containsKey(nums[i]))
            {
                hashMap.put(nums[i],hashMap.get(nums[i])+1);
            }
            else{
                hashMap.put(nums[i],1);
            }
        }
        for (Integer integer : hashMap.keySet()) {
            System.out.println(integer + "  "  + hashMap.get(integer));
        }

        for (int i = 0; i < len; i++) {
            System.out.print(nums[i] + "  ");
        }
        System.out.println();
        for (int i = 0; i < len; i++) {
            if(nums[i] > target) break;
            if(i > 0 && nums[i] == nums[i-1])
                continue;
            int L = i + 1;
            int R = len - 1;
            while (L < R){
                int sum = nums[i] + nums[L] + nums[R];
                int count = 0;
                if(sum == target){

                    if(nums[i] == target - sum)
                        count++;
                    if(nums[L] == target - sum)
                        count++;
                    if(nums[R] == target - sum)
                        count++;
                    for (int j = L+1; j < R ; j++) {

                        if(nums[j] == (target - sum) && hashMap.get(nums[j]) - count > 0){
                            ans.add(Arrays.asList(nums[i],nums[L],nums[R],nums[j]));
                            while(L < R && nums[L] == nums[L+1])
                                L++;
                            while(L < R && nums[R] == nums[R-1]){
                                R--;
                            }
                            break;
                        }
                        if(nums[j] > 0){
                            R--;
                        }
                        if(nums[j] < 0){
                            L++;
                        }
                    }

                }
                else if(sum < target){
                    if(nums[i] == target - sum)
                        count++;
                    if(nums[L] == target - sum)
                        count++;
                    if(nums[R] == target - sum)
                        count++;
                    for (int j = L+1; j < R ; j++) {
                        if(nums[j] == (target - sum) && hashMap.get(nums[j]) - count > 0){
                            ans.add(Arrays.asList(nums[i],nums[L],nums[R],nums[j]));
                            break;
                        }
                    }
                    while(L < R && nums[L] == nums[L+1])
                        L++;
                    L++;
                }
                else if(sum > target){
                    if(nums[i] == target - sum)
                        count++;
                    if(nums[L] == target - sum)
                        count++;
                    if(nums[R] == target - sum)
                        count++;
                    for (int j = L+1; j < R ; j++) {
                        if(nums[j] == (target - sum) && hashMap.get(nums[j]) - count > 0){
                            ans.add(Arrays.asList(nums[i],nums[L],nums[R],nums[j]));
                            break;
                        }
                    }
                    while(L < R && nums[R] == nums[R-1]){
                        R--;
                    }
                    R--;
                }

            }
        }
        return ans;
    }
}*/